package io.chenglicun.modules.clc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.modules.clc.dao.ClcPartyMemberScoreDao;
import io.chenglicun.modules.clc.dto.ClcPartyMemberScoreDTO;
import io.chenglicun.modules.clc.entity.ClcPartyMemberScoreEntity;
import io.chenglicun.modules.clc.service.ClcPartyMemberScoreService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 党员积分
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-30
 */
@Service
public class ClcPartyMemberScoreServiceImpl extends CrudServiceImpl<ClcPartyMemberScoreDao, ClcPartyMemberScoreEntity, ClcPartyMemberScoreDTO> implements ClcPartyMemberScoreService {

    @Override
    public QueryWrapper<ClcPartyMemberScoreEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcPartyMemberScoreEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    @Transactional
    public void addScore(Long userId, Integer score) {
        ClcPartyMemberScoreEntity entity = baseDao.selectOne(
                new QueryWrapper<ClcPartyMemberScoreEntity>()
                        .lambda().eq(ClcPartyMemberScoreEntity::getUserId, userId)
                        .last("limit 1")
        );
        if (entity != null) {
            entity.setScore(entity.getScore() + score);
            this.updateById(entity);
        } else {
            entity = new ClcPartyMemberScoreEntity();
            entity.setUserId(userId);
            entity.setScore(score);
            this.insert(entity);
        }
    }
}